﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Biblioteca.Utils;
using System.Data.OracleClient;
using System.IO;
using SERIX.Classes;


namespace SERIX
{
    public partial class cripto : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridPreencher(ref GridBens, "E");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Grids("E",ref GridBens);
        }

        /// <summary>
        /// Metodo para inserir ou alterar os dados
        /// </summary>
        /// <param name="tpo"></param>
        /// <param name="Grid"></param>
        protected void Grids(string tpo, ref GridView Grid)
        {
            DataTable tb = CBensPropriedades.PesquisarPorPropriedade("1", "ADM");
            foreach (GridViewRow row in Grid.Rows)
            {                
                if (((CheckBox)row.FindControl("CheckBens")).Checked == true)
                {       
                    string quant = ((TextBox)row.FindControl("tbQuantidade")).Text;
                    string quant1 = ((TextBox)row.FindControl("tbQuantidade1")).Text;
                    string quant2 = ((TextBox)row.FindControl("tbQuantidade2")).Text;
                    string bem = Grid.Rows[row.RowIndex].Cells[1].Text;
                    string idt_desc = CBensPropriedades.PesquisarPorIdtDesc(Grid.DataKeys[row.RowIndex]["IDT_DESC"].ToString(), "ADM").Idt_desc;
                    if (idt_desc == null)
                    {
                        idt_desc = Grid.DataKeys[row.RowIndex]["IDT_DESC"].ToString();
                        CBensPropriedades dao = new CBensPropriedades(CBensPropriedades.ObterIdt(), "1", idt_desc, quant, DateTime.Now.Year.ToString(), tpo, bem, "", "", quant1, quant2);
                        dao.Incluir("ADM", false);
                    }
                    else
                    {
                        string IDT_BENS_PROPRIEDADES = CBensPropriedades.PesquisarPorIdtDesc(Grid.DataKeys[row.RowIndex]["IDT_DESC"].ToString(), "ADM").Idt_Bens_prop;
                        CBensPropriedades dao = new CBensPropriedades(IDT_BENS_PROPRIEDADES, "1", idt_desc, quant, DateTime.Now.Year.ToString(), tpo, bem, "", "", quant1, quant2);
                        dao.Alterar("ADM", "Q");
                    }
                }
                else
                {
                    int linha = tb.Rows.Count;
                    for (int i = 0; i < linha; i++)
                    {
                        string idt_desc = Grid.DataKeys[row.RowIndex]["IDT_DESC"].ToString();
                        if (idt_desc == tb.Rows[i]["IDT_DESC"].ToString())
                        {
                            CBensPropriedades dao = CBensPropriedades.PesquisarPorIdtDesc(idt_desc, "ADM");
                            dao.Alterar("ADM", "D");
                        }
                    }
                }

            }
        }
        protected void GridBens_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Page.IsPostBack)
            {
                CarregarGrid(ref GridBens);
            }
        }

        /// <summary>
        /// Metodo para encher o grid com informações da propriedade
        /// </summary>
        /// <param name="Grid"></param>
        private void CarregarGrid(ref GridView Grid)
        {
            DataTable tb = CBensPropriedades.PesquisarPorPropriedade("1", "ADM");
            foreach (GridViewRow row in Grid.Rows)
            {
                CheckBox cb = ((CheckBox)row.FindControl("CheckBens"));
                TextBox tb0 = ((TextBox)row.FindControl("tbQuantidade"));
                TextBox tb1 = ((TextBox)row.FindControl("tbQuantidade1"));
                TextBox tb2 = ((TextBox)row.FindControl("tbQuantidade2"));
                int linha = tb.Rows.Count;
                for(int i = 0; i<linha;i++)
                {
                    if (Grid.DataKeys[row.RowIndex]["IDT_DESC"].ToString() == tb.Rows[i]["IDT_DESC"].ToString())
                    {
                        cb.Checked = true;
                        tb0.Text = tb.Rows[i]["QUANT_PROP"].ToString();
                        tb1.Text = tb.Rows[i]["QUANT2_PROP"].ToString();
                        tb2.Text = tb.Rows[i]["QUANT3_PROP"].ToString();
                    }
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            GridPreencher(ref GridBens, "E");
        }

        protected void GridBens_DataBound(object sender, EventArgs e)
        {
            CarregarGrid(ref GridBens);
        }
        /// <summary>
        /// metodo para preencher o grid pela primeira vez
        /// </summary>
        /// <param name="Grid"></param>
        /// <param name="tpo"></param>
        protected void GridPreencher(ref GridView Grid, string tpo)
        {
            DataTable tb = CBensPropriedades.PreencherGrid(tpo,false);
            Grid.DataSource = tb;
            Grid.DataBind();
            foreach(GridViewRow row in Grid.Rows)
            {
                Label lb = (Label)row.FindControl("lbUnidade");
                Label lb1 = (Label)row.FindControl("lbUnidade1");
                Label lb2 = (Label)row.FindControl("lbUnidade2");
                TextBox tb1 = (TextBox)row.FindControl("tbQuantidade1");
                TextBox tb2 = (TextBox)row.FindControl("tbQuantidade2");

                lb.Text = tb.Rows[row.RowIndex][3].ToString();
                lb1.Text = tb.Rows[row.RowIndex][4].ToString();
                if (lb1.Text == "")
                {
                    tb1.Visible = false;
                }
                lb2.Text = tb.Rows[row.RowIndex][5].ToString();
                if (lb2.Text == "")
                {
                    tb2.Visible = false;
                }
            }
        }

    }
}
